package com.txunda.zhuohired.aty.user.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.ants.theantsgo.gson.GsonUtil;
import com.ants.theantsgo.util.L;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.adapter.user.home.ExpectJobSelectAdapter;
import com.txunda.zhuohired.base.BaseAty;
import com.txunda.zhuohired.bean.user.resume.ExpectBean;
import com.txunda.zhuohired.http.pst.MemberPst;
import com.txunda.zhuohired.view.FlowLayout;
import com.txunda.zhuohired.view.TagAdapter;
import com.txunda.zhuohired.view.TagFlowLayout;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2018/3/2 10:03
 * 功能描述：期望职位
 * 联系方式：jiahuanchen@126.com
 */

public class ExpectJobAty extends BaseAty {
    /**
     * 标题
     */
    @ViewInject(R.id.tv_layout_white_center)
    private TextView tv_layout_white_center;
    /**
     * 保存
     */
    @ViewInject(R.id.tv_layout_white_right)
    private TextView tv_layout_white_right;
    /**
     * 热门职位
     */
    @ViewInject(R.id.id_listview)
    private ListView id_listview;
    /**
     * 详细职位
     */
    @ViewInject(R.id.id_flowlayout)
    private TagFlowLayout id_flowlayout;
    /**
     * 选中职位
     */
    @ViewInject(R.id.tf_select)
    private TagFlowLayout tf_select;
    /**
     * 标题栏
     */
    private ArrayList<ExpectBean.DataBean.PositionBean> selectlist;
    private ArrayList<ExpectBean.DataBean.PositionBean> detaillist;
    private Set<Integer> downlist;
    private TagAdapter<ExpectBean.DataBean.PositionBean> detil_tagAdapter;
    private ExpectJobSelectAdapter expectJobSelectAdapter;
    private MemberPst memberPst;
    ExpectBean expectJobBean;
    private ArrayList<String> expectIdList = new ArrayList<>();
    private ArrayList<String> expectNameList = new ArrayList<>();


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showStatusBar(R.id.layout_white_head);
        tv_layout_white_center.setText("期望职位");
        tv_layout_white_right.setVisibility(View.VISIBLE);
        tv_layout_white_right.setText("保存");
    }

    @Override
    @OnClick({R.id.tv_layout_white_right})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_layout_white_right://保存
                if (selectlist.size() == 0) {
                    showErrorTip("请选择期望职位");
                    return;
                }
                for (int i = 0; i < selectlist.size(); i++) {
                    expectIdList.add(selectlist.get(i).getId());
                    expectNameList.add(selectlist.get(i).getName());
                }
                Intent intent = new Intent();
                intent.putStringArrayListExtra("expect_job_id", expectIdList);
                intent.putStringArrayListExtra("expect_job_name", expectNameList);
                L.e("=====expectIdList=====", expectIdList.toString());
                L.e("=====expectNameList=====", expectNameList.toString());
                setResult(RESULT_OK, intent);
                finish();
                break;
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.aty_expect_job;
    }

    @Override
    protected void initialized() {
        memberPst = new MemberPst(this);
        selectlist = new ArrayList();
        detaillist = new ArrayList();
        downlist = new HashSet();
        final LayoutInflater mInflater = LayoutInflater.from(this);
        final TagAdapter selectlistAdapter = new TagAdapter<ExpectBean.DataBean.PositionBean>(selectlist) {

            @Override
            public View getView(FlowLayout parent, int position, ExpectBean.DataBean.PositionBean s) {
                TextView tv = (TextView) mInflater.inflate(R.layout.tag_flow_layouttv_select,
                        tf_select, false);
                tv.setText(s.getName() + "    ×");
                return tv;
            }
        };
        tf_select.setAdapter(selectlistAdapter);
        tf_select.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                selectlist.remove(selectlist.get(position));
                selectlistAdapter.notifyDataChanged();
                notifyList();
                return true;
            }
        });


        detil_tagAdapter = new TagAdapter<ExpectBean.DataBean.PositionBean>(detaillist) {

            @Override
            public View getView(FlowLayout parent, int position, ExpectBean.DataBean.PositionBean s) {
                TextView tv = (TextView) mInflater.inflate(R.layout.tag_flow_layouttv,
                        id_flowlayout, false);
                tv.setText(s.getName());
                return tv;
            }
        };
        id_flowlayout.setAdapter(detil_tagAdapter);
        id_flowlayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                if (containsBean(detaillist.get(position), selectlist)) {
                    removeBean(detaillist.get(position), selectlist);
                    selectlistAdapter.notifyDataChanged();
                } else {
                    if (selectlist.size() >= 5) {
                        Toast.makeText(ExpectJobAty.this, "已选择5个", Toast.LENGTH_SHORT).show();
                        Set<Integer> selectedList = id_flowlayout.getSelectedList();
                        selectedList.remove(position);
                        id_flowlayout.getAdapter().setSelectedList(selectedList);
                    } else {
                        selectlist.add(detaillist.get(position));
                        selectlistAdapter.notifyDataChanged();
                    }
                }
                return true;
            }
        });
    }

    @Override
    protected void requestData() {
        memberPst.position();//获取职位
    }

    private boolean containsBean(ExpectBean.DataBean.PositionBean bean, ArrayList<ExpectBean.DataBean.PositionBean>
            list) {
        boolean isContains = false;
        for (ExpectBean.DataBean.PositionBean job : list
                ) {
            if (job.getId().equals(bean.getId()))
                isContains = true;
        }
        return isContains;
    }

    private void removeBean(ExpectBean.DataBean.PositionBean bean, ArrayList<ExpectBean.DataBean.PositionBean> list) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId().equals(bean.getId()))
                list.remove(list.get(i));
            i--;
        }
    }

    private void notifyList() {
        downlist.clear();
        for (int i = 0; i < detaillist.size(); i++) {
            if (containsBean(detaillist.get(i), selectlist))
                downlist.add(i);
        }
        detil_tagAdapter.setSelectedList(downlist);
        detil_tagAdapter.notifyDataChanged();
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        if (requestUrl.contains("position")) {
            L.e("==========position==========", jsonStr);
            ExpectBean expectJobBean = GsonUtil.GsonToBean(jsonStr, ExpectBean.class);
            initData(expectJobBean);
            return;
        }
    }

    private void initData(final ExpectBean expectJobBean) {
        expectJobSelectAdapter = new ExpectJobSelectAdapter(this, expectJobBean.getData());
        id_listview.setAdapter(expectJobSelectAdapter);
        id_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                expectJobSelectAdapter.setSelectPosition(i);
                detaillist.clear();
                detaillist.addAll(expectJobBean.getData().get(i).getPosition());
                notifyList();
            }


        });
        detaillist.addAll(expectJobBean.getData().get(0).getPosition());
        notifyList();
    }
}
